package demo12;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class MapJoinJob {
    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop10:9000");

        Job job = Job.getInstance(conf);
        job.setJarByClass(MapJoinJob.class);

        FileInputFormat.addInputPath(job,new Path("mapreduce/demo12/order.txt"));
        FileOutputFormat.setOutputPath(job,new Path("mapreduce/demo12/out"));

        job.setMapperClass(MapJoinMapper.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setNumReduceTasks(0);

        job.addCacheFile(new URI("/mapreduce/demo12/pd.txt"));

        job.waitForCompletion(true);
    }

    static class MapJoinMapper extends Mapper<LongWritable, Text,Text, NullWritable>{
        private Map<String,String> pdMap = new HashMap<>();

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            // 固定写法
            URI[] cacheFiles = context.getCacheFiles();
            FileSystem fileSystem = FileSystem.get(context.getConfiguration());
            FSDataInputStream fs = fileSystem.open(new Path(cacheFiles[0]));
            BufferedReader br = new BufferedReader(new InputStreamReader(fs,"UTF-8"));

            String line;
            while (StringUtils.isNotBlank((line = br.readLine()))){
                String[] arr = line.split("\t");
                pdMap.put(arr[0],arr[1]);
            }
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] arr = value.toString().split("\t");
            String pname = pdMap.get(arr[1]);
            context.write(new Text(arr[0] + "\t" + pname + "\t" + arr[2]), NullWritable.get());
        }
    }
}
